#include "StdAfx.h"
#include "Material.h"

namespace Renderer
{

	Material::Material()
	{
		ZeroMemory(&material, sizeof(D3DMATERIAL9));
		SetDiffuseColor(255, 255, 255);
		SetAmbientColor(255, 255, 255);
		SetSpecularColor(255, 255, 255);
		SetEmissiveColor(0, 0, 0);
		SetIntensity(1.0f);
	}

	D3DMATERIAL9* Material::GetMaterial()
	{
		return &material;
	}

	void Material::SetDiffuseColor(BYTE red, BYTE green, BYTE blue)
	{
		material.Diffuse.r = 1.0f / 255.0f * float(red);
		material.Diffuse.g = 1.0f / 255.0f * float(green);
		material.Diffuse.b = 1.0f / 255.0f * float(blue);
	}
	void Material::GetDiffuseColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (BYTE)(255 * material.Diffuse.r);

		if(green != NULL)
			*green = (BYTE)(255 * material.Diffuse.g);

		if(blue != NULL)
			*blue = (BYTE)(255 * material.Diffuse.b);
	}

	void Material::SetAmbientColor(BYTE red, BYTE green, BYTE blue)
	{
		material.Ambient.r = 1.0f / 255.0f * float(red);
		material.Ambient.g = 1.0f / 255.0f * float(green);
		material.Ambient.b = 1.0f / 255.0f * float(blue);
	}

	void Material::GetAmbientColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (BYTE)(255.0f * material.Ambient.r);

		if(green != NULL)
			*green = (BYTE)(255.0f * material.Ambient.g);

		if(blue != NULL)
			*blue = (BYTE)(255.0f * material.Ambient.b);
	}

	void Material::SetSpecularColor(BYTE red, BYTE green, BYTE blue)
	{
		material.Specular.r = 1.0f / 255.0f * float(red);
		material.Specular.g = 1.0f / 255.0f * float(green);
		material.Specular.b = 1.0f / 255.0f * float(blue);
	}

	void Material::GetSpecularColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (BYTE)(255.0f * material.Specular.r);

		if(green != NULL)
			*green = (BYTE)(255.0f * material.Specular.g);

		if(blue != NULL)
			*blue = (BYTE)(255.0f * material.Specular.b);
	}

	void Material::SetEmissiveColor(BYTE red, BYTE green, BYTE blue)
	{
		material.Emissive.r = 1.0f / 255.0f * float(red);
		material.Emissive.g = 1.0f / 255.0f * float(green);
		material.Emissive.b = 1.0f / 255.0f * float(blue);
	}
	void Material::GetEmissiveColor(BYTE *red, BYTE *green, BYTE *blue) const
	{
		if(red != NULL)
			*red = (BYTE)(255.0f * material.Emissive.r);

		if(green != NULL)
			*green = (BYTE)(255.0f * material.Emissive.g);

		if(blue != NULL)
			*blue = (BYTE)(255.0f * material.Emissive.b);
	}

	void Material::SetIntensity(float intensity)
	{
		material.Power = intensity;
	}
	float Material::GetIntensity() const
	{
		return material.Power;
	}

}